package com.hitales.ma.medicalrecord.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

@Configuration
public class JacksonConfig {

    @Bean
    public MappingJackson2HttpMessageConverter customJacksonConverter() {
        ObjectMapper objectMapper = new ObjectMapper();

        //添加模块
        SimpleFilterProvider filters = new SimpleFilterProvider();
        filters.addFilter("globalCustomFilter", new GlobalCustomPropertyFilter());
        objectMapper.setFilterProvider(filters);
        objectMapper.addMixIn(Object.class, GlobalMixin.class);

        // 自定义 Jackson 配置
        objectMapper.enable(SerializationFeature.INDENT_OUTPUT); // 格式化 JSON
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); // 处理时间格式

        return new MappingJackson2HttpMessageConverter(objectMapper);
    }

    @Bean
    public ObjectMapper customObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();

        // 绑定全局过滤器
        objectMapper.setFilterProvider(new SimpleFilterProvider()
                .addFilter("globalCustomFilter", new GlobalCustomPropertyFilter()));

        objectMapper.addMixIn(Object.class, GlobalMixin.class);

        return objectMapper;
    }

    // 让所有类默认使用全局过滤器
    public abstract static class GlobalMixin {}
}
